package com.sinosoft.service.impl.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sinosoft.common.CommonConst;
import com.sinosoft.entity.DefRole;
import com.sinosoft.exception.ComplainException;
import com.sinosoft.mapper.DefRoleMapper;
import com.sinosoft.service.system.IRoleService;

@Service
public class RoleServiceImpl implements IRoleService {
	@Autowired
	private DefRoleMapper defRoleMapper;

	@Override
	public Map<String, Object> selectRoleByCondition(DefRole defRole) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<DefRole> list = defRoleMapper.selectByCondition(defRole);
		for (DefRole mDefRole : list) {
			mDefRole.setRcState(CommonConst.RC_STATE.get(mDefRole.getRcState()));
		}
		resultMap.put("rows", list);
		resultMap.put("total", defRoleMapper.selectByConditionCount(defRole));
		return resultMap;
	}

	@Transactional
	@Override
	public Map<String, Object> roleAdd(DefRole role) {
		Map<String, Object> map = new HashMap<String, Object>();
		int n = defRoleMapper.roleAdd(role);
		if (n == 1) {
			map.put("flag", true);
			map.put("msg", "添加成功！");
		} else {
			throw new ComplainException("角色添加失败！插入动作影响行数不等于1条！");
		}
		return map;
	}

	@Transactional
	@Override
	public Map<String, Object> roleModify(DefRole role) {
		Map<String, Object> map = new HashMap<String, Object>();
		int n = defRoleMapper.roleModify(role);
		if (n == 1) {
			map.put("flag", true);
			map.put("msg", "修改角色成功！");
		} else {
			throw new ComplainException("角色修改失败！修改动作影响行数不等于1条！");
		}
		return map;
	}

	@Transactional
	@Override
	public Map<String, Object> roleDelete(Long roleId) {
		Map<String, Object> map = new HashMap<String, Object>();
		int n = defRoleMapper.roleDelete(roleId);
		if (n == 1) {
			map.put("flag", true);
			map.put("msg", "删除成功！");
		} else {
			throw new ComplainException("删除失败！删除操作影响的行数不等于1！");
		}
		return map;
	}

}
